<?php
/*
+--------------------------------------------------------------------------
|   Anwsion [#RELEASE_VERSION#]
|   ========================================
|   by Tatfook Network Team
|   (c) 2011 - 2012 Anwsion Software
|   http://www.anwsion.com
|   ========================================
|   Support: zhengqiang@gmail.com
|   
+---------------------------------------------------------------------------
*/

class FORMAT
{

	/**
	 *  字符进行安全转向处理 
	 * @param string $request
	 * @param boolean $istext
	 */
	static function safe($request, $istext = false)
	{
		// If we are on PHP >= 6.0.0 we do not need some code
		if (version_compare(PHP_VERSION, '6.0.0', '<'))
		{
			$request = addslashes($request); //转意义
		}
		
		if ($istext === true)
		{
			$request = strip_tags(str_replace(array(
				'<', 
				'>'
			), array(
				'&lt;', 
				'&gt;'
			), $request)); //括号 
		}
		
		return $request;
	}

	/**
	 * 
	 * 格式化内容显示
	 * @param string $content
	 * @param int    $charset_num
	 * 
	 * @return array $content = array('content_title' => 截断后内容, 'content_content' => 实际内容 )
	 */
	public static function format_content($content, $charset_num = 200, $dot = '...')
	{
		$content_title = '';
		$content_conent = strip_tags($content);
		$content_len = strlen($content_conent);
		
		if ($content_len > $charset_num)
		{
			$content_title = cjk_substr($content_conent, 0, $charset_num, 'UTF-8', $dot);
		}
		else
		{
			$content_title = '';
		}
		
		return array(
			'content_title' => $content_title, 
			'content_content' => $content
		);
	}

	static function parse_links($str, $popup = TRUE)
	{
		$str = @preg_replace('/(http[s]?:\/\/(?!' . $_SERVER['HTTP_HOST'] . ')[-a-zA-Z0-9@:;%_\+.~#?\&\/\/=\x80-\xff]+)/i', '<a href="\1" class="a" rel="nofollow" target="_blank">\1</a>', $str);
		
		$str = @preg_replace('/(http[s]?:\/\/'.$_SERVER['HTTP_HOST'].'[-a-zA-Z0-9@:;%_\+.~#?&\/\/=\x80-\xff]*)/i', '<a href="\1" class="a">\1</a>', $str);
		
		if (strpos($str, "http") === FALSE)
		{
			$str = @preg_replace('/((?!' . $_SERVER['HTTP_HOST'] . ')www\.[-a-zA-Z0-9@:;%_\+\.~#?&\/\/=]+)/i', '<a href="http://\1" class="a" rel="nofollow" target="_blank">\1</a>', $str);
			
			if(preg_match('/^www\./', $_SERVER['HTTP_HOST']))
			{
				$str = @preg_replace('/('.$_SERVER['HTTP_HOST'].'[-a-zA-Z0-9@:;%_\+\.~#?&\/\/=]*)/i', '<a href="http://\1" class="a">\1</a>', $str);
			}
		}
		
		$str = @preg_replace('/([a-z0-9\+_\-]+[\.]?[a-z0-9\+_\-]+@[a-z0-9\-]+\.+[a-z]{2,6})/is', '<a href="mailto:\1">\1</a>', $str);	//解析EMAIL地址

		$str = @preg_replace_callback('/\[url([^\[]*)\[\/url\]/i', 'restri_url', $str);

		return $str;
	}

	static function outside_url_exists($str)
	{
		if (preg_match('/(http[s]?:\/\/(?!' . $_SERVER['HTTP_HOST'] . ')[-a-zA-Z0-9@:;%_\+.~#?&\/\/=]+)/i', $str))
		{
			return true;
		}
		else
		{
			if(preg_match('/((?!' . $_SERVER['HTTP_HOST'] . ')www\.[-a-zA-Z0-9@:;%_\+\.~#?&\/\/=]+)/i', $str))
			{
				return true;
			}
			else
			{
				return false;
			}
		}
	}
	
	static function parse_attachs($str, $get_attachs_id = false)
	{
		if($get_attachs_id)
		{
			preg_match_all('/\[attach\]([0-9]+)\[\/attach]/', $str, $matches);
			
			return array_unique($matches[1]);
		}
		else
		{
			return preg_replace('/\[attach\]([0-9]+)\[\/attach]/', "<script type=\"text/javascript\" src=\"" . get_js_url('/question/ajax/load_attach/') . "\$1\"></script>", $str);
		}
	}
}